package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("FeaturestandardController")
public class FeaturestandardController extends CmiEmaController {

	@RequestMapping("/listFeaturestandards")
	public ModelAndView listFeaturestandards(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = featurestandardPaginationService.updatePagination(request, pagination);
		Set<Featurestandard> featurestandards = featurestandardPaginationService
				.getPaginationFeaturestandards(pagination);

		mav.addObject("featurestandards", featurestandards);
		mav.addObject("entityName", "featurestandard");
		mav.addObject("pagination", pagination);
		mav.setViewName("featurestandard/listFeaturestandards.jsp");
		return mav;
	}

	public String listFeaturestandards() {
		return "redirect:/listFeaturestandards";
	}

	@RequestMapping("/viewFeaturestandard")
	public ModelAndView viewFeaturestandard(@RequestParam Integer featurestandardId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("featurestandard", featurestandardDAO.findFeaturestandardByPrimaryKey(featurestandardId));
		mav.setViewName("featurestandard/viewFeaturestandard.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFeaturestandards")
	public ModelAndView confirmDeleteFeaturestandards(
			@RequestParam(value = "deleteFeaturestandardsIds", required = true) Set<Integer> deleteFeaturestandardsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Featurestandard> deleteFeaturestandards = featurestandardDAO
				.findFeaturestandardsByIds(deleteFeaturestandardsIds);
		Set<Featurestandard> referencedFeaturestandards = featurestandardService
				.loadReferenceFeaturestandards(deleteFeaturestandards);
		boolean hasReferenced;
		if (referencedFeaturestandards.size() == 0) {
			mav.addObject("deleteEntitys", deleteFeaturestandards);
			hasReferenced = false;
		} else {
			deleteFeaturestandards.removeAll(referencedFeaturestandards);
			mav.addObject("referencedEntitys", referencedFeaturestandards);
			mav.addObject("deleteEntitys", deleteFeaturestandards);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Featurestandard");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFeaturestandards");
		return mav;
	}

	@RequestMapping("/deleteFeaturestandard")
	public String deleteFeaturestandard(@RequestParam Integer featurestandardId) {
		Featurestandard featurestandard = featurestandardDAO.findFeaturestandardByPrimaryKey(featurestandardId);
		featurestandardService.deleteFeaturestandard(featurestandard);
		return "forward:/listFeaturestandards";
	}

	@RequestMapping("/confirmDeleteFeaturestandard")
	public ModelAndView confirmDeleteFeaturestandard(@RequestParam Integer featurestandardId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("featurestandard", featurestandardDAO.findFeaturestandardByPrimaryKey(featurestandardId));

		mav.setViewName("featurestandard/deleteFeaturestandard.jsp");

		return mav;
	}

	@RequestMapping("/deleteFeaturestandards")
	public String deleteFeaturestandards(
			@RequestParam(value = "deleteFeaturestandardsId", required = true) Set<Integer> deleteFeaturestandardsId) {
		featurestandardService.deleteFeaturestandards(deleteFeaturestandardsId);
		return "forward:/listFeaturestandards";
	}

	@RequestMapping("/editFeaturestandard")
	public ModelAndView editFeaturestandard(@RequestParam Integer featurestandardId) {

		ModelAndView mav = new ModelAndView();

		Featurestandard featurestandard = featurestandardDAO.findFeaturestandardByPrimaryKey(featurestandardId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("featurestandard", featurestandard);

		// 把所有的引用featurestandard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(featurestandardService.loadFeaturestandardObjectReferencedSet(featurestandard));

		// 将编辑页面featurestandard添加到mav中
		mav.setViewName("featurestandard/editFeaturestandard.jsp");
		return mav;
	}

	@RequestMapping("/newFeaturestandard")
	public ModelAndView newFeaturestandard() {
		ModelAndView mav = new ModelAndView();
		Featurestandard featurestandard = new Featurestandard();

		mav.addObject("featurestandard", featurestandard);

		// 把所有的引用featurestandard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(featurestandardService.loadFeaturestandardObjectReferencedSet(featurestandard));

		mav.addObject("newFlag", true);
		mav.setViewName("featurestandard/editFeaturestandard.jsp");

		return mav;
	}

	@RequestMapping("/saveFeaturestandard")
	public ModelAndView saveFeaturestandard(@ModelAttribute Featurestandard featurestandard) {

		ModelAndView mav = new ModelAndView();

		Feature featureTemp = featurestandard.getFeature();
		Feature feature = featureTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(featureDAO.findFeatureById(featureTemp.getId()));
		Standard standardTemp = featurestandard.getStandard();
		Standard standard = standardTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(standardDAO.findStandardById(standardTemp.getId()));
		Unit unitTemp = featurestandard.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Featurestandard existingFeaturestandard = CmiHibernateProxyUtil.deproxy(featurestandardDAO
				.findFeaturestandardByPhysicalPrimaryKey(unit.getId(), standard.getId(), feature.getId()));

		featurestandard.setFeature(feature);
		featurestandard.setStandard(standard);
		featurestandard.setUnit(unit);
		// 新建记录
		if (featurestandard.getId() == null) {
			if (featurestandard.equals(existingFeaturestandard)) {

				mav.addAllObjects(featurestandardService.loadFeaturestandardClassReferencedSet(featurestandard));
				// mav = editContinue(mav, featurestandard);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("featurestandard/editFeaturestandard.jsp");
			} else {
				featurestandardService.saveFeaturestandard(featurestandard);
				mav.setViewName("forward:/listFeaturestandards");
			}
		}
		// 编辑记录
		else {
			Featurestandard originFeaturestandard = CmiHibernateProxyUtil
					.deproxy(featurestandardDAO.findFeaturestandardByPrimaryKey(featurestandard.getId()));
			if (!featurestandard.equals(originFeaturestandard) && featurestandard.equals(existingFeaturestandard)) {
				mav.addAllObjects(featurestandardService.loadFeaturestandardClassReferencedSet(featurestandard));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("featurestandard/editFeaturestandard.jsp");
			} else {
				featurestandardService.saveFeaturestandard(featurestandard);
				mav.setViewName("forward:/listFeaturestandards");
			}
		}
		return mav;
	}

	@RequestMapping("/featurestandardController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
